package handle

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handle/request"
	"net/http"

	"github.com/gin-gonic/gin"
)

func CreatedDiscount(c *gin.Context) {
	var req request.CreatedDiscount
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":  http.StatusBadRequest,
			"msg":   "参数错误",
			"error": err.Error(),
		})
		return
	}
	userId := c.GetUint("userId")
	discount, err := config.Discount.CreatedDiscount(c, &__.CreatedDiscountReq{
		Name:          req.Name,
		IsLimitPut:    req.IsLimitPut,
		PutCount:      req.PutCount,
		UseCouponType: req.UseCouponType,
		StartTime:     req.StartTime,
		EndTime:       req.EndTime,
		DayNum:        req.DayNum,
		CouponType:    req.CouponType,
		Money:         float32(req.Money),
		SpendMony:     float32(req.SpendMony),
		Discount:      req.Discount,
		TotalMoney:    float32(req.TotalMoney),
		UseGoodsType:  req.UseGoodsType,
		PromotionWay:  req.PromotionWay,
		GetType:       req.GetType,
		LimitCount:    req.LimitCount,
		UserId:        int64(userId),
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":  http.StatusInternalServerError,
			"msg":   err.Error(),
			"error": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "OK",
		"Data": discount.Message,
	})

}

func DiscountList(c *gin.Context) {
	var req request.DiscountList
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":  http.StatusBadRequest,
			"msg":   "参数错误",
			"error": err.Error(),
		})
		return
	}
	//userId := c.GetUint("userId")
	list, err := config.Discount.DiscountList(c, &__.DiscountListReq{
		Page:     req.Page,
		SizePage: req.SizePage,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":  http.StatusInternalServerError,
			"msg":   err.Error(),
			"error": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "OK",
		"Data": list.List,
	})
}

func DiscountDel(c *gin.Context) {
	var req request.DiscountDel
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":  http.StatusBadRequest,
			"msg":   "参数错误",
			"error": err.Error(),
		})
		return
	}
	userId := c.GetUint("userId")
	del, err := config.Discount.DiscountDel(c, &__.DiscountDelReq{
		DiscountId: req.DiscountId,
		UserId:     int64(userId),
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":  http.StatusInternalServerError,
			"msg":   err.Error(),
			"error": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "OK",
		"Data": del.Message,
	})
}
